package cn.livingCloud.entity.core;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.entity.user.UserAccount;
import cn.livingCloud.repository.EmployeeAccountMapper;
import cn.livingCloud.utils.SessionUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.SimpleAuthenticationInfo;

import java.util.Date;

/**
 * Created by 唐旭 on 2016/6/1.
 */
public class EmployeeAccount extends UserAccount {

    private transient EmployeeAccountMapper mapper = BeanProvider.getBean(EmployeeAccountMapper.class);

    private String employeeUuid;//员工ID
    private Employee employee;

    public EmployeeAccount() {
    }

    public String getEmployeeUuid() {
        return employeeUuid;
    }

    public EmployeeAccount setEmployeeUuid(String employeeUuid) {
        this.employeeUuid = employeeUuid;
        return this;
    }

    public Employee getEmployee() {
        return employee;
    }

    public EmployeeAccount setEmployee(Employee employee) {
        this.employee = employee;
        return this;
    }

    @Override
    public AuthenticationInfo login() {
        this.setLastLoginTime(new Date());
        mapper.updateLastLoginTime(this);
        SessionUtils.setAttribute(SessionConstants.USER_ACCOUNT_KEY, this);
        SessionUtils.setAttribute(SessionConstants.EMPLOYEE_KEY, employee);
        return new SimpleAuthenticationInfo(this.getUsername(), this.getPassword(), REALM_NAME);
    }

    @Override
    public String getRole() {
        return "EMPLOYEE";
    }
}
